package com.ruoyi.common.utils;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;
import org.slf4j.Logger;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class AliCloudOssUtil {

    private static Logger log = org.slf4j.LoggerFactory.getLogger(AliCloudOssUtil.class);

    private String bucketName;
    private String keyPrefix;
    private OSS ossClient;



    public AliCloudOssUtil(String ak,
                           String sk,
                           String endpoint,
                           String bucketName,
                           String keyPrefix) {
        this.bucketName = bucketName;
        this.keyPrefix = keyPrefix;
        this.ossClient = new OSSClientBuilder().build(endpoint,
                CredentialsProviderFactory.newDefaultCredentialProvider(ak, sk));
    }

    /**
     * Closes the OssClient by shutting it down.
     *
     * @return None
     */

    public void closeOssClient() {
        this.ossClient.shutdown();
    }


    /**
     * Retrieves a list of keys for CSV files in a given bucket and key prefix.
     *
     * @return         	Returns a list of strings representing the keys of CSV files.
     */
    public List<String> getListCsvFileKeys() {
        List<String> csvFileKeys = new ArrayList<>();
        try {
            ObjectListing objectListing = ossClient.listObjects(bucketName, keyPrefix);
            List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
            for (OSSObjectSummary s : sums) {
                log.info("\t" + s.getKey());
                String key = s.getKey();
                if (key.matches(".*/(alpha|beta|gamma|delta|theta)\\.csv$")) {
                    csvFileKeys.add(key);
                }

            }
        } catch (OSSException oe) {
            log.info("Caught an OSSException");
            log.info("Error Message: " + oe.getErrorMessage());
            log.info("Error Code: " + oe.getErrorCode());
            log.info("Request ID: " + oe.getRequestId());
            log.info("Host ID: " + oe.getHostId());
        } catch (ClientException ce) {
            log.info("Caught an ClientException");
            log.info("Error Message: " + ce.getMessage());
        }

        return csvFileKeys;
    }


    final  String tmpPath = "/var/tmp/";
    public void downloadCsvFile(String key) {
        ossClient.getObject(new GetObjectRequest(bucketName, key), new File(tmpPath + key));
    }

    public List<File> batchDownloadCsvFile(List<String> keys, String tmpPath) {
        List<File> files = new ArrayList<>();

        for (String key : keys) {
            File file = new File(tmpPath + key);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            files.add(file);
            ossClient.getObject(new GetObjectRequest(bucketName, key), file);
        }

        return files;

    }


    public List<File> batchDownloadCsvFile(List<String> keys) {
        List<File> files = new ArrayList<>();;

        for (String key : keys) {
            //key : \project_98\eeg\p08-jycy-0621-result-0\p08-jycy-0621-result-0\alpha.csv
            File file = new File(tmpPath + key);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            files.add(file);
            ossClient.getObject(new GetObjectRequest(bucketName, key), file);
        }
        return files;
    }
}